package logic

import (
	"context"
	"database/sql"
	"game/model"

	"game/rpc/gamerpc/game"
	"game/rpc/gamerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *game.UpdateUserRequest) (*game.UpdateUserResponse, error) {
	// 构造用户数据
	user := &model.Users{
		Id:       in.User.Id,
		Username: in.User.Username,
		Password: in.User.Password,
		Email:    sql.NullString{String: in.User.Email, Valid: in.User.Email != ""},
	}

	// 更新用户数据
	err := l.svcCtx.UsersModel.Update(l.ctx, user)
	if err != nil {
		return nil, err
	}

	// 返回成功响应
	return &game.UpdateUserResponse{
		Success: true,
	}, nil
}
